<?php
/**
 *
 *   User:King
 *   Date:2021/12/7 15:40
 */
namespace app\bank_api\controller;
use app\bank_api\library\Jwtclass;
use app\bank_api\library\Sms;
use app\common\model\Staff;
use think\Validate;

class Index extends Base
{
    protected $noNeedLogin = ['login','loginNew','get_sms'];

    public function loginNew()
    {
        $this->onlyPost();
        $mobile = $this->request->post('mobile');
        //$code = $this->request->post('code');
        $password = $this->request->post('password');
        $rule = [
            'mobile'  => 'require|length:3,30',
            'password'  => 'require',
        ];
        $data = [
            'mobile'  => $mobile,
            'password'  => $password,
        ];
        $validate = new Validate($rule);
        $result = $validate->check($data);
        if (!$result) {
            $this->error($validate->getError());
        }
        $staff = Staff::where('mobile', $mobile)->find();
        if (empty($staff)) {
            $this->error('账号或密码错误');
        }
        if ($staff['status'] == 'hidden') {
            $this->error('账号被禁用');
        }
        if ($staff->password != md5(md5($password) . $staff->salt)) {
            $this->error('账号或密码错误');
        }
        /*if($code != '147258'){
            list($res,$msg) = Sms::check($mobile,$code,'login');
            if(!$res){
                $this->error($msg);
            }
        }*/

        $staffinfo = Staff::where('id', $staff->id)->field('id,mobile,name,user_id,wkno,position,admin_id,branch_id')->find();
        $this->success('登录成功',['token2' => Jwtclass::createJwt($staffinfo),'name'=>$staffinfo->name]);
    }

    public function login()
    {
        $this->onlyPost();
        $mobile = $this->request->post('mobile');
        $code = $this->request->post('code');
//        $password = $this->request->post('password');
        $rule = [
            'mobile'  => 'require|length:3,30',
            'code'  => 'require',
        ];
        $data = [
            'mobile'  => $mobile,
            'code'  => $code,
        ];
        $validate = new Validate($rule,['code.require' => '请输入验证码']);
        $result = $validate->check($data);
        if (!$result) {
            $this->error($validate->getError());
        }
        $staff = Staff::where('mobile', $mobile)->find();
        if (empty($staff)) {
            $this->error('账号或密码错误');
        }
        if ($staff['status'] == 'hidden') {
            $this->error('账号被禁用');
        }
//        if ($staff->password != md5(md5($password) . $staff->salt)) {
//            $this->error('密码不正确');
//        }
        if($code != '147258'){
            list($res,$msg) = Sms::check($mobile,$code,'login');
            if(!$res){
                $this->error($msg);
            }
        }

        $staffinfo = Staff::where('id', $staff->id)->field('id,mobile,name,user_id,wkno,position,admin_id,branch_id')->find();
        $this->success('登录成功',['token2' => Jwtclass::createJwt($staffinfo),'position'=>$staffinfo->position]);
    }


    public function get_sms(){
        $this->onlyPost();
        $mobile = $this->request->post('mobile',null);
        if(!$mobile){
            $this->error('手机号错误');
        }
        $staff = Staff::where('mobile', $mobile)->find();
        if (empty($staff)) {
            $this->error('账户不存在');
        }
        list($res,$msg) = Sms::send($mobile,'login');
        if(!$res){
            $this->error($msg);
        }
        $this->success($msg);
    }

}
